/*
 * @Author: C.
 * @Date: 2021-08-30 16:48:45
 * @LastEditTime: 2021-09-07 09:37:16
 * @Description: file content
 */
import { buildUUID } from './index';
import { defaultSize } from '../maps';
import { EleStyle, FieldInfo, TableInfo, PrintTemplate } from '../types';

// 元素构造器
export const FieldConstructor = function (fieldObj: Partial<FieldInfo>): FieldInfo {
  return Object.assign(
    {
      role: 'label',
      label: '新字段',
      prop: fieldObj.prop || '',
      filter: fieldObj.filter || 'default',
      url: fieldObj.url || '',
      id: fieldObj.role + buildUUID() + new Date().getTime(),
      show: true,
      style: defaultStyle(fieldObj.role),
    },
    fieldObj
  );
};

// 默认样式构造器
function defaultStyle(role): EleStyle {
  const size = defaultSize[role];
  return {
    y: 0,
    x: 0,
    w: size[0],
    h: size[1],
    fontFamily: 'inherit',
    fontSize: 16,
    fontStyle: ['normal', 'normal', 'none'],
    textAlign: 'center',
    fontWeight: 'normal',
  };
}

// 表格元素构造器
export const TableConstructor = function (prop: string, title?: string): TableInfo {
  return {
    id: 'table' + buildUUID(),
    show: true,
    prop: prop,
    showSum: false,
    title: FieldConstructor({
      role: 'label',
      label: title || '',
    }),

    fields: [],
  };
};

export const printTemConstructor = function (): PrintTemplate {
  return {
    paperStyle: {
      // 纸张类型
      paperType: 'A4',
      // 纸张方向
      direction: 'portrait',
      // 左边距
      marginLeft: 0,
      // 右边距
      marginRight: 0,
      // 上边距
      marginTop: 0,
      // 下边距
      marginBottom: 0,
    },
    header: {
      style: {
        w: 794,
        h: 250,
      },
      nodes: [
        {
          id: '12123132',
          show: true,
          label: '测试11111',
          prop: 'string',
          role: 'label',
          style: {
            x: 100,
            y: 100,
            h: 100,
            w: 100,
            fontFamily: 'inherit',
            fontSize: 16,
            textAlign: 'center',
            fontWeight: 'normal',
          },
        },
        {
          id: '1212313292',
          show: true,
          label: '测试22222',
          prop: 'string',
          role: 'value',
          style: {
            x: 100,
            y: 100,
            h: 100,
            w: 100,
            fontFamily: 'inherit',
            fontSize: 16,
            textAlign: 'center',
            fontWeight: 'normal',
          },
        },
      ],
    },
    body: {
      style: {
        w: 794,
        h: 250,
      },
      nodes: [
        {
          id: '12123132',
          show: true,
          label: '测试33333',
          prop: 'string',
          role: 'label',
          style: {
            x: 0,
            y: 100,
            h: 50,
            w: 100,
            fontFamily: 'inherit',
            fontSize: 21,
            textAlign: 'center',
            fontWeight: 'normal',
          },
        },
      ],
    },
    // 表格设置
    table: [
      {
        id: 'sdfsderew',
        show: false,
        prop: 'string',
        showSum: false,
        title: FieldConstructor({ role: 'value', label: '表格标题' }),
        fields: [
          [
            {
              id: 'dsfdf',
              show: true,
              label: '测试1',
              prop: 'string',
              role: 'label',
              style: {
                x: 0,
                y: 100,
                h: 50,
                w: 100,
                fontFamily: 'inherit',
                fontSize: 17,
                textAlign: 'center',
                fontWeight: 'normal',
              },
            },
            {
              id: '12123oi4554p132',
              show: true,
              label: '测试2',
              prop: 'string',
              role: 'value',
              style: {
                x: 0,
                y: 100,
                h: 50,
                w: 100,
                fontFamily: 'inherit',
                fontSize: 12,
                textAlign: 'center',
                fontWeight: 'normal',
              },
            },
          ],
          [
            {
              id: '12123oop1832',
              show: true,
              label: '测试3',
              prop: 'string',
              role: 'label',
              style: {
                x: 0,
                y: 100,
                h: 50,
                w: 100,
                fontFamily: 'inherit',
                fontSize: 17,
                textAlign: 'center',
                fontWeight: 'normal',
              },
            },
            {
              id: '1f2123oi4554p132',
              show: true,
              label: '测试4',
              prop: 'string',
              role: 'value',
              style: {
                x: 0,
                y: 100,
                h: 50,
                w: 100,
                fontFamily: 'inherit',
                fontSize: 12,
                textAlign: 'center',
                fontWeight: 'normal',
              },
            },
          ],
        ],
      },

      {
        id: 'werwergh',
        show: true,
        prop: 'string',
        showSum: false,
        title: FieldConstructor({ role: 'label', label: '表格2' }),
        fields: [
          [
            {
              id: '12123oop132',
              show: true,
              label: '测试1',
              prop: 'string',
              role: 'label',
              style: {
                x: 0,
                y: 100,
                h: 50,
                w: 100,
                fontFamily: 'inherit',
                fontSize: 17,
                textAlign: 'center',
                fontWeight: 'normal',
              },
            },
            {
              id: '12123oi4554p132',
              show: true,
              label: '测试2',
              prop: 'string',
              role: 'value',
              style: {
                x: 0,
                y: 100,
                h: 50,
                w: 100,
                fontFamily: 'inherit',
                fontSize: 12,
                textAlign: 'center',
                fontWeight: 'normal',
              },
            },
          ],
          [
            {
              id: '12123oop1832',
              show: true,
              label: '测试3',
              prop: 'string',
              role: 'label',
              style: {
                x: 0,
                y: 100,
                h: 50,
                w: 100,
                fontFamily: 'inherit',
                fontSize: 17,
                textAlign: 'center',
                fontWeight: 'normal',
              },
            },
            {
              id: '1f2123oi4554p132',
              show: true,
              label: '测试4',
              prop: 'string',
              role: 'value',
              style: {
                x: 0,
                y: 100,
                h: 50,
                w: 100,
                fontFamily: 'inherit',
                fontSize: 12,
                textAlign: 'center',
                fontWeight: 'normal',
              },
            },
          ],
        ],
      },
    ],

    footer: {
      style: {
        w: 794,
        h: 250,
      },
      nodes: [],
    },
  };
};
